/**
 * controller_activity.js
 * 活动相关控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-12
 */

//活动列表
app.controller('ActivityList', function($scope, $http, $location, common){
	//变量声明
	$scope.list 	= [];
	$scope.pageHtml = '';
	$scope.nPage 	= 1;
	$scope.loading 	= false;
	$scope.level 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		//标签页钩子设置
        $('a[data-toggle="tab"]').on('show.bs.tab', function(e){
            var type    = $(this).data('target');
            switch(type){
                case '#list-all': $scope.level 	= false; break;
                case '#list-1'	: $scope.level 	= 1; break;
                case '#list-2'	: $scope.level 	= 2; break;
                case '#list-3'	: $scope.level 	= 3; break;
            }
            $scope.load_data(1);
        });
	}

	//加载数据
	$scope.load_data 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/Activity/getList?p='+page;
		if($scope.level !== false){
			url 	+= '&level='+$scope.level;
		}

		$http.get(url).success(function(info){
			$scope.loading 	= false;
			
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml = info.data.page_html;

			common.startPage($scope.load_data);
		})

		$scope.loading 	= true;
		$scope.list 	= new Array();
	}

	//修改数据
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/Activity/updateItem';
		var param 	= {
			'activity_id'	: id,
			'activity_info'	: info
		}

		$http.post(url, param).success(function(info){
			$scope.loading 	= false;

			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.load_data($scope.nPage);
		})

		$scope.loading 	= true;
	}

	//详情信息
	$scope.get_item 	= function(id){
		S('activity_id', id);
		$location.path('/Activity/getItem');
	}

	//添加条目
	$scope.add_item 	= function(){
		$location.path('/Activity/addItem');
	}

	//活动须知
	$scope.get_known 	= function(item){
		S('activity_info', item);
		$location.path('/Activity/getKnown');
	}

	$scope.load_data();
	$scope.init_js();
})

//添加活动
app.controller('ActivityAdd', function($scope, $http, $location, common){
	//变量声明
	$scope.nItem 	= {};
	$scope.loading 	= false;

	//富文本框变量声明
	var eIntroduct, eContent, eIheader, eIfooter, eIIcont, eIIIcont;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-i-audio').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-i-audio':
							$scope.nItem.i_audio 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});

		eIntroduct 	= common.createEditor('introduct');
		eContent 	= common.createEditor('content');
		eIheader 	= common.createEditor('i-header');
		eIfooter 	= common.createEditor('i-footer');
		eIIcont 	= common.createEditor('ii-cont');
		eIIIcont 	= common.createEditor('iii-cont');
	}

	//添加条目
	$scope.add_item 	= function(){
		var url 	= config.prefix_url + '/Activity/addItem';
		var item 	= $scope.nItem;

		item.introduct 	= eIntroduct.$txt.html();
		item.content 	= eContent.$txt.html();
		item.i_header 	= eIheader.$txt.html();
		item.i_footer 	= eIfooter.$txt.html();
		item.ii_cont 	= eIIcont.$txt.html();
		item.iii_cont 	= eIIIcont.$txt.html();
		
		$http.post(url, item).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('添加成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/Activity/getList');
	}

	$scope.init_js();
})

//活动详情
app.controller('ActivityGet', function($scope, $http, $location, common){
	$scope.itemId 	= S('activity_id');
	$scope.nItem 	= {};

	if(!$scope.itemId){
		common.doAlert('活动ID无效!');
		$scope.back_url();
	}

	//富文本框变量声明
	var eIntroduct, eContent, eIheader, eIfooter, eIIcont, eIIIcont;

	//初始化JS
	$scope.init_js 	= function(){
		//上传插件
		$('#upload-banner, #upload-audio, #upload-i-audio').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-banner':
							$scope.nItem.banner 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-audio':
							$scope.nItem.audio 	= config.host_name + '/server' + file.path;
						break;
						case 'upload-i-audio':
							$scope.nItem.i_audio 	= config.host_name + '/server' + file.path;
						break;
					}
				})
			}
		});

		eIntroduct 	= common.createEditor('introduct');
		eContent 	= common.createEditor('content');
		eIheader 	= common.createEditor('i-header');
		eIfooter 	= common.createEditor('i-footer');
		eIIcont 	= common.createEditor('ii-cont');
		eIIIcont 	= common.createEditor('iii-cont');

	}

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/Activity/getItem?activity_id='+$scope.itemId;

		$http.get(url).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.nItem 	= info.data.info;

			var info 	= info.data.info;
			//富文本框添加
			eIntroduct.$txt.html(info.introduct);
			eContent.$txt.html(info.content);
			eIheader.$txt.html(info.i_header);
			eIfooter.$txt.html(info.i_footer);
			eIIcont.$txt.html(info.ii_cont);
			eIIIcont.$txt.html(info.iii_cont);
		})

		$scope.loading 	= true;
	}

	//更新数据
	$scope.update_item 	= function(){
		var url 	= config.prefix_url + '/Activity/updateItem';
		var item 	= $scope.nItem;
		item.introduct 	= eIntroduct.$txt.html();
		item.content 	= eContent.$txt.html();
		item.i_header 	= eIheader.$txt.html();
		item.i_footer 	= eIfooter.$txt.html();
		item.ii_cont 	= eIIcont.$txt.html();
		item.iii_cont 	= eIIIcont.$txt.html();
		var param 	= {
			'activity_id'	: item.id,
			'activity_info'	: item
		};
		
		$http.post(url, param).success(function(info){
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('更新成功');
			$scope.back_url();
		})

		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/Activity/getList');
	}

	$scope.load_data();
	$scope.init_js();
})

//活动须知
app.controller('ActivityKnown', function($scope, $http, $location, common){
	//变量声明
	$scope.activityInfo 	= S('activity_info');
	$scope.list 	= [];
	$scope.nItem 	= {};
	$scope.loading 	= false;

	if($scope.activityInfo == false){
		common.doAlert('活动信息获取失败!');
		$location.path('/Activity/getList');
	}

	//初始化JS
	$scope.init_js 	= function(){
		$('#upload-audio-update, #upload-audio-add').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var info 	= eval('('+info+')');
				var domId 	= $(this).attr('id');
				var file 	= info.data.info;
				file.path 	= config.host_name + '/server' + file.path;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-audio-update':
							$scope.nItem.audio 	= file.path;
						break;
						case 'upload-audio-add':
							$scope.aItem.audio 	= file.path;
						break;
					}
				})
			}
		});
	}

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/Activity/getKnownList?activity_id='+$scope.activityInfo.id;

		$http.get(url).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
		})
		$scope.loading 	= true;
	}

	//须知详情
	$scope.get_item 	= function(item){
		$scope.nItem 	= item;
		$('.modal.get-known').modal('show');
	}

	//变更信息
	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/Activity/updateKnown';
		var param 	= {
			'known_id'	: id,
			'known_info'	: info 
		}
		console.log(param);
		$http.post(url, param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.load_data(0);
			$('.modal.get-known').modal('hide');
		})

		$scope.loading 	= true;
	}

	//添加信息
	$scope.add_item 	= function(item){
		var url 	= config.prefix_url + '/Activity/addKnown';
		var param 	= item;
			param.activity_id 	= $scope.activityInfo.id;
		
		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.loading 	= false;
			$scope.load_data();
			$('.modal.add-known').modal('hide');
		})
		$scope.loading 	= true;
	}

	//返回列表
	$scope.back_url 	= function(){
		$location.path('/Activity/getList');
	}

	$scope.load_data();
	$scope.init_js();
})

//活动相册
app.controller('ActivityPictureList', function($scope, $http, $location, common){
	//变量声明
	$scope.list 	= new Array();
	$scope.nPage 	= 1;
	$scope.pageHtml 	= '';
	$scope.loading 	= false;
	$scope.keyword 	= '';
	$scope.dateS 	= '';
	$scope.dateE 	= '';

	//初始化JS
	$scope.init_js 	= function(){
		$('.input-date').datetimepicker({
            language:  'zh-CN',
            format: 'yyyy-mm-dd',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            showMeridian: 1,
            minView : 2
        });

        //回车键监听
        common.listenEnterKey(function(){
            $scope.get_list(1);
        });
	}

	//加载数据
	$scope.get_list 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/Activity/getPictureList?p='+page;
		var param 	= '&date_start='+$scope.dateS+
					  '&date_end='+$scope.dateE+
					  '&keyword='+$scope.keyword;

		$http.get(url+param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.pageHtml 	= info.data.page_html;
			$scope.nPage 	= page;

			common.startPage($scope.get_list);
		})

		$scope.loading 	= true;
	}

	//图片显示
	$scope.show_picture 	= function(path){
		$('.modal.modal-image').children('img').attr('src', path);
		$('.modal.modal-image').modal('show');
	}

	$scope.init_js();
	$scope.get_list();
})